'  ==========================================================================
'  |                                                                        |
'  | SQLitening Example A                                                   |
'  |                                                                        |
'  ==========================================================================
'  This is a simple demonstration that shows how to open and
'  use an SQLite database in both local and remote modes. Remote
'  mode requires that you have already started the SQLitening
'  server, otherwise an error will occur.
'  ==========================================================================
'  PUBLIC DOMAIN SOFTWARE
'  The author or authors of this code dedicate any and all
'  copyright interest in this code to the public domain.
'  Anyone is free to copy, modify, publish, use, compile,
'  sell, or distribute the original code, either in source
'  code form or as a compiled binary, for any purpose,
'  commercial or non-commercial, and by any means.
'  Fred Meier - July 2010
'  ==========================================================================
#Dim All

#Compile Exe "..\Bin\ExampleA.Exe"
#Include "..\Inc\SQLitening.Inc"

'==============================<[ Main ]>==============================
Function PbMain()

   Local NumRecs As Long
   Local lStart  As Double
   Local lEnd    As Double
   Local st      As String
   
   
   ' Connect to the server -- omit this call to run in local mode
   ' or use the slSetProcessMods function to explicitly switch to
   ' local mode without dropping the server connection.  No server
   ' name will default to LocalHost.
   slConnect

   
   '//
   '//  TEST #1  - Client/Server remote mode
   '//
   
   ' Save our starting time
   lStart = Timer
   
   ' Open the sample database
   slOpen "sample.db3"

   ' Select record set (should be 10,000 rows returned)
   slSel "Select * from Parts"

   ' Process records
   NumRecs = 0
   Do While slGetRow
      Incr NumRecs
      ' When a row is returned, you use the slF, slFN, slFX, slFNX functions. 
      ' Example:
      ' lsA = slF(1) & ", " & slF(2) & "," & slFN("Product")
   loop

   ' Save the time it took to run this test so we can display it later.
   lEnd = Timer - lStart
   
   ' Build the string that will display the results
   st = st & _
        "Test 1:  Retrieve " & Format$(NumRecs) & " rows from server (LocalHost)." & _
        "  Time: " & Format$(lEnd, "##.0000") & " seconds." & $CrLf & $CrLf
   


   '//
   '//  TEST #2  - Local mode (bypass Client/Server remote mode)
   '//

   ' Push the Database and switch to local mode. Local mode deals directly with the
   ' database on the client machine. It does not send commands to the server so it
   ' should be faster than remote mode).
   slPushDatabase
   slSetProcessMods "L0"

   ' Save our starting time
   lStart = Timer

   ' Open

   slOpen "sample.db3"

   ' Select record set
   slSel "Select * from Parts"

   ' Process records
   NumRecs = 0
   Do While slGetRow
      Incr NumRecs
      ' When a row is returned, you use the slF, slFN, slFX, slFNX functions. 
      ' Example:
      ' lsA = slF(1) & ", " & slF(2) & "," & slFN("Product")
   Loop

   ' Save the time it took to run this test so we can display it later.
   lEnd = Timer - lStart
   
   ' Continue to build the string that will display the results
   st = st & _
        "Test 2:  Retrieve " & Format$(NumRecs) & " rows locally." & _
        "  Time: " & Format$(lEnd, "##.0000") & " seconds." & $CrLf & $CrLf



   '//
   '//  TEST #3  - Client/Server remote mode again
   '//

   ' Pop the Database and switch back to remote mode.
   slPopDatabase
   slSetProcessMods "L1"

   ' Select record set
   slSel "Select * from Parts"

   ' Process records
   NumRecs = 0
   Do While slGetRow
      Incr NumRecs
      ' When a row is returned, you use the slF, slFN, slFX, slFNX functions. 
      ' Example:
      ' lsA = slF(1) & ", " & slF(2) & "," & slFN("Product")
   Loop

   ' Save the time it took to run this test so we can display it later.
   lEnd = Timer - lStart
   
   ' Continue to build the string that will display the results
   ' Build the string that will display the results
   st = st & _
        "Test 3:  Retrieve " & Format$(NumRecs) & " rows from server (LocalHost)." & _
        "  Time: " & Format$(lEnd, "##.0000") & " seconds." & $CrLf & $CrLf & _
        "See how much faster local is? Remote (client/server) mode is fast, but local " & _
        "mode is super fast!"
        
   ? st
   
End Function
